package com.shop.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.constant.MessageConstant;
import com.shop.entity.QueryPageBean;
import com.shop.entity.Result;
import com.shop.pojo.Manager;
import com.shop.service.ManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/manager")
@Api(tags = "管理员管理")
public class ManagerController {
    @Reference
    private ManagerService managerService;

    @PostMapping("/add")
    @ApiOperation("添加管理员接口")
    public Result add(@RequestBody Manager manager){
        if (managerService.findManagerName(manager.getName())!=null){
            return new Result(false,MessageConstant.ADD_MANAGER_FAIL);
        }else if (managerService.findUserName(manager.getUsername())!=null){
            return new Result(false,MessageConstant.ADD_USERNAME_FAIL);
        } else{
            managerService.add(manager);
            return new Result(true,MessageConstant.ADD_MANAGER_SUCCESS);
        }
    }

    @PreAuthorize("hasAuthority('QUERY_MANAGER')")
    @PostMapping("/pageQuery")
    @ApiOperation("管理员分页接口")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        Page<Manager> page=managerService.pageQuery(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        return new Result(true,MessageConstant.QUERY_MANAGER_SUCCESS,page);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除管理员接口")
    @ApiImplicitParams({@ApiImplicitParam(dataType = "int",name = "id",value = "管理员id",required = true,example = "0")})
    public Result delete(Integer id){
        try {
            managerService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MANAGER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_MANAGER_SUCCESS);
    }

    @PutMapping("/edit")
    @ApiOperation("修改管理员接口")
    public Result edit(@RequestBody Manager manager){
//        try {
//            managerService.edit(manager);
//        }catch (Exception e){
//            e.printStackTrace();
//            return new Result(false,MessageConstant.EDIT_MANAGER_FAIL);
//        }
//        return new Result(true,MessageConstant.EDIT_MANAGER_SUCCESS);
        Manager m = managerService.findById(manager.getId());
        if (managerService.findManagerName(manager.getName())!=null && !m.getName().equals(manager.getName())){
            return new Result(false,MessageConstant.EDIT_MANAGER_FAIL);
        }else if(managerService.findUserName(manager.getUsername())!=null && !m.getUsername().equals(manager.getUsername())){
            return new Result(false,MessageConstant.EDIT_USERNAME_FAIL);
        }else{
            managerService.edit(manager);
            return new Result(true,MessageConstant.EDIT_MANAGER_SUCCESS);
        }
    }

    @GetMapping("/findById")
    @ApiOperation("根据管理员id查找管理员接口")
    public Result findById(@RequestParam(required = true) Integer id){
        try {
            Manager manager=managerService.findById(id);
            return new Result(true,MessageConstant.QUERY_MANAGER_SUCCESS,manager);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MANAGER_FAIL);
        }
    }

    @GetMapping("/findAll")
    @ApiOperation("显示管理员下拉列表接口")
    public Result findAll(){
        try {
            List<Manager> list=managerService.findAll();
            return new Result(true,MessageConstant.QUERY_MANAGER_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MANAGER_FAIL);
        }
    }

}
